package com.clgc.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clgc.baseservice.exception.MyException;
import com.clgc.eduservice.entity.EduCourse;
import com.clgc.eduservice.entity.EduTopic;
import com.clgc.eduservice.entity.excel.TopicExcel;
import com.clgc.eduservice.service.EduCourseService;
import com.clgc.eduservice.service.EduTopicService;
import com.clgc.utils.ResultCode;
import com.clgc.utils.TopicType;

import javax.ws.rs.HEAD;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.StringJoiner;

/**
 * @author lsr
 * @create 2021-04-27-14:50
 */
public class ExcelListener extends AnalysisEventListener<TopicExcel> {

    private EduTopicService topicService;

    private EduCourseService courseService;

    public ExcelListener(EduTopicService topicService, EduCourseService courseService) {
        this.topicService = topicService;
        this.courseService = courseService;
    }

    @Override
    public void invoke(TopicExcel topicExcel, AnalysisContext analysisContext) {

        if (topicExcel == null) {
            throw new MyException("传入文件为空", ResultCode.ERROR);
        }

        Class<? extends TopicExcel> clazz = topicExcel.getClass();
        Field[] fields = clazz.getDeclaredFields();
        StringJoiner stringJoiner = new StringJoiner("￥￥￥");
        for (int i = 2; i < 12; i++) {
            try {
                PropertyDescriptor descriptor = new PropertyDescriptor(fields[i].getName(), clazz);
                Method readMethod = descriptor.getReadMethod();
                Object invoke = readMethod.invoke(topicExcel);
                if (invoke != null) {
                    stringJoiner.add((char) ('A' + (i - 2)) + invoke.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper();
        queryWrapper.eq("title", topicExcel.getCourseTitle());
        queryWrapper.select("id");
        EduCourse eduCourse = courseService.getOne(queryWrapper);
        if (eduCourse == null) {
            throw new MyException("没有找到" + topicExcel.getCourseTitle() + "对应课程，请重新选择，或添加该课程后进行此操作", ResultCode.ERROR);
        }
        if (!Arrays.asList(TopicType.topicType).contains(topicExcel.getType())) {
            throw new MyException(topicExcel.getTopicSubject() + "====该题题型不正确", ResultCode.ERROR);
        }
        ;
        EduTopic eduTopic = new EduTopic();
        eduTopic.setAnswer(topicExcel.getAnwser());
        eduTopic.setTopicOption(stringJoiner.toString());
        eduTopic.setTopicSubject(topicExcel.getTopicSubject());
        eduTopic.setKind(topicExcel.getType());
        eduTopic.setCourseId(eduCourse.getId());

        boolean flag = topicService.save(eduTopic);
        if (!flag) {
            throw new MyException("添加题库出错", ResultCode.ERROR);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
